package com.crystal.jcms.ui;

import com.crystal.constant.Constant;
import com.crystal.jcms.shpz.R;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

/**
 *	底部栏
 *
 * 	@author Lean  @date:2014-8-4  
 */
public class BottomPanelLayout extends LinearLayout implements OnClickListener{
	
	private ImageText mMenuBtn;
	private ImageText mSetBtn;
	private ImageText mScanBtn;
	private BottomPanelCallBackProtocal mCallBackProtocal;
	
	private LinearLayout container;
	private View divider;
	
	//代理协议
	public void setCallBackProtocal(BottomPanelCallBackProtocal callBackProtocal) {
		this.mCallBackProtocal = callBackProtocal;
	}

	public BottomPanelLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public interface BottomPanelCallBackProtocal{
		public void onClickCallBack(int itemID);
	}
	
	
	public void initBottomPanel(){
		initItemBtn(mMenuBtn,Constant.STR_FRAGMENT_APPS,R.drawable.menu);
		initItemBtn(mSetBtn,Constant.STR_FRAGMENT_SET,R.drawable.more);
	}

	private void initItemBtn(ImageText itv,String text,int resId) {
		if (itv!=null) {
			itv.setText(text);
			itv.setImage(resId);
			itv.setBackgroundColor(Color.parseColor("#4086f6"));
		}
	}
	
	public void defaultChecked(){
		//mMenuBtn.setChecked(Constant.SIGN_FRAGMENT_APPS);
		mMenuBtn.setBackgroundColor(Color.parseColor("#1a64db"));
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		divider = (View)findViewById(R.id.divider);
		container = (LinearLayout)findViewById(R.id.container_btn);
		mMenuBtn=(ImageText) findViewById(R.id.menu_btn);
		mSetBtn=(ImageText) findViewById(R.id.set_btn);
		initClickEvent();
	}

	private void initClickEvent() {
		int n=container.getChildCount();
		if (n==0) {
			return;
		}
		for (int i = 0; i < n; i++) {
			container.getChildAt(i).setOnClickListener(this);
		}
	}

	/**  
	 * 1.修改本身样式 
	 * 2.对外声明事件
	 */
	@Override
	public void onClick(View v) {
		initBottomPanel();
		v.setBackgroundColor(Color.parseColor("#1a64db"));
		int index=-1;
		switch (v.getId()) {
			case R.id.menu_btn:
				index=Constant.SIGN_FRAGMENT_APPS;
				//mMenuBtn.setChecked(index);
				break;
			case R.id.set_btn:
				index=Constant.SIGN_FRAGMENT_SET;
				//mSetBtn.setChecked(index);
				break;
		}
		if (mCallBackProtocal!=null) {
			mCallBackProtocal.onClickCallBack(index);
		}
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		//layoutItem(l, t, r, b);
	}

	private void layoutItem(int left, int top, int right, int bottom) {
		int allChildWidth=0;
		int num=getChildCount();
		for (int i = 0; i < num; i++) {
			allChildWidth+=getChildAt(i).getWidth();
		}
		int absoluteWidth=right-left-getPaddingLeft()-getPaddingRight();
		int blankWidth=(absoluteWidth-allChildWidth)/(num-1);
		//设置第2 3个按钮的间距
		LayoutParams params1=(LayoutParams) mSetBtn.getLayoutParams();
		params1.leftMargin=blankWidth;
		mSetBtn.setLayoutParams(params1);
		LayoutParams params2=(LayoutParams) mScanBtn.getLayoutParams();
		params2.leftMargin=blankWidth;
		mScanBtn.setLayoutParams(params2);
	}
	

}
